<template>
  <el-dialog :visible.sync="dialog" :title="isAdd ? '新增岗位' : '编辑岗位'" append-to-body width="500px" :close-on-click-modal="false">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
      <!-- <el-form-item label="岗位名称" prop="name">
        <el-input v-model="form.name" style="width: 370px;" placeholder="岗位名称以ROLE_开头，否则系统将不能识别"/>
      </el-form-item> -->
      <el-form-item label="岗位名称" prop="nickname">
        <el-input v-model="form.nickname" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="所属部门">
        <treeselect v-model="form.deptId" :options="depts" :default-expand-level="1" style="width: 370px" placeholder="选择部门" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number v-model.number="form.sort" :min="0" :max="999" controls-position="right" style="width: 370px;"/>
      </el-form-item>
      <!-- <el-form-item label="岗位级别" prop="level">
        <el-input-number v-model.number="form.level" :min="1" controls-position="right"/>
      </el-form-item> -->
      <!-- <el-form-item label="数据范围">
        <el-select v-model="form.dataScope" placeholder="请选择数据范围" @change="changeScope">
          <el-option
            v-for="item in dateScopes"
            :key="item"
            :label="item"
            :value="item"/>
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item v-if="form.dataScope === '自定义'" label="数据权限">
        <treeselect v-model="deptIds" :options="depts" multiple style="width: 370px" placeholder="请选择" />
      </el-form-item> -->
      <el-form-item label="描述信息">
        <el-input v-model="form.remark" style="width: 370px;" rows="5" type="textarea"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getDepts } from '@/api/dept'
import { add, edit } from '@/api/role'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  components: { Treeselect },
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    sup_this: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      dateScopes: ['全部', '本级', '自定义'],
      loading: false, 
      dialog: false, 
      depts: [], 
      // deptIds: [],
      form: { 
        name: '', 
        nickname: '',
        deptId: null,
        sort: 0,
        remark: '', 
        dataScope: '本级', 
        level: 2
      },
      rules: {
        nickname: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      if (this.form.dataScope === '自定义') {
        this.$message({
          message: '自定义数据权限不能为空',
          type: 'warning'
        })
      } else {
        // if (this.form.dataScope === '自定义') {
        //   for (let i = 0; i < this.deptIds.length; i++) {
        //     this.form.depts.push({
        //       id: this.deptIds[i]
        //     })
        //   }
        // }
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.loading = true
            if (this.isAdd) {
              this.doAdd()
            } else {
              this.doEdit()
            }
          } else {
            this.$message({
              message: '上级部门不能为空',
              type: 'warning'
            })
          }
        })
      }
    },
    doAdd() {
      add(this.form).then(res => {
        this.resetForm()
        this.$message({
          message: '岗位添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.$parent.$parent.$parent.init()
      }).catch(err => {
        this.loading = false
      })
    },
    doEdit() {
      edit(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        this.$notify({
          title: err.msg,
          type: 'danger',
          duration: 2500
        })
        this.loading = false
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = { name: '', depts: [], remark: '', dataScope: '本级', level: 3 }
    },
    getDepts() {
      getDepts({ enabled: 1 }).then(res => {
        this.depts = res.data.list
      })
    },
    changeScope() {
      if (this.form.dataScope === '自定义') {
        // this.getDepts()
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  /deep/ .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
